﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_Reports_Accounts_AccountBooks_DebitNote : System.Web.UI.Page
{
    Datautility du = new Datautility();
    AccountSettings ac = new AccountSettings();
    double dblTotalDebit = 0;
    double dblTotalCredit = 0;
    int firstrow = 1;
    string priviousbill;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            lblcompname.Text = Session["compname"].ToString();
            txtFromDate.Text = "01/04/" + Session["FinYear"].ToString();
            txtToDate.Text = "31/03/" +(Convert.ToInt32( Session["FinYear"].ToString()) + 1).ToString();
            hidFinyear.Value = Session["Finyear"].ToString();
            getJournalDetails(txtFromDate.Text, txtToDate.Text);
          
        }

    }
    protected void getJournalDetails(string strFromDate, string strToDate)
    {
        if (strFromDate != string.Empty && strToDate != string.Empty)
        {
           
            SqlParameter[] pr ={
                         new SqlParameter("@FromDate",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(strFromDate))),
                        new SqlParameter("@ToDate",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(strToDate))),
                        new SqlParameter("@TransactionTypeList","3")};
            lblheading.Text = "Debit Note Books From " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strFromDate)) + " To " +string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(strToDate));
            DataTable dt = du.getdatausingprocedure("USP_ACC_AccountBookVoucherDetails", pr);
            grdDisplay.DataSource = dt;
            grdDisplay.DataBind();
        }

       
    }
    protected void rowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[4].Text != string.Empty)
            {
                dblTotalDebit += Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text));
                if (Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text)) != 0)
                {
                    e.Row.Cells[4].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text)));
                }
                else
                {
                    e.Row.Cells[4].Text = string.Empty;
                }
 
            }
            if (e.Row.Cells[5].Text != string.Empty)
            {
                dblTotalCredit += Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text));
                if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) != 0)
                {
                    e.Row.Cells[5].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)));
                }
                else
                {
                    e.Row.Cells[5].Text = string.Empty;
                }

            }
           
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dv = (DataRowView)e.Row.DataItem;
            if (priviousbill == dv["TransactionNumber"].ToString())
            {
                if (grdDisplay.Rows[firstrow].Cells[2].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[2].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[2].RowSpan += 1;
                }
                if (grdDisplay.Rows[firstrow].Cells[1].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[1].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[1].RowSpan += 1;
                }
                if (grdDisplay.Rows[firstrow].Cells[0].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[0].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[0].RowSpan += 1;
                }
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
                e.Row.Cells.RemoveAt(0);
            }
            else
            {
                e.Row.VerticalAlign = VerticalAlign.Middle;
                priviousbill = dv["TransactionNumber"].ToString();
                firstrow = e.Row.RowIndex;
            }

        }
        if(e.Row.RowType== DataControlRowType.Footer)
            {
                e.Row.Cells[3].Text="Total";
                e.Row.Cells[4].Text = ac.GetCurrency(dblTotalDebit).ToString();
                e.Row.Cells[5].Text = ac.GetCurrency(dblTotalCredit).ToString();
                e.Row.Font.Bold = true;
                e.Row.HorizontalAlign = HorizontalAlign.Right;
            }

    }
    
}
